package com.fdb.efp.nls.service.impl;

import com.fdb.basic.framework.core.base.FrameworkService;
import com.fdb.basic.sequence.service.facade.SequenceService;
import com.fdb.efp.nls.common.LimitStatusEnum;
import com.fdb.efp.nls.service.dao.QuotaLimitPrdDao;
import com.fdb.efp.nls.service.domain.QuotaLimitPrd;
import com.fdb.efp.nls.service.facade.QuotaLimitPrdService;
import com.fdb.efp.nls.service.vo.QuotaLimitPrdVO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

/**
 * Service Impl: 产品额度限制表服务实现
 *
 * @author gcb
 * @date 2020-06-12
 */
@Service("quotaLimitPrdService")
public class QuotaLimitPrdServiceImpl implements QuotaLimitPrdService, FrameworkService {

    private static Logger logger = LoggerFactory.getLogger(QuotaLimitPrdServiceImpl.class);

    @Autowired
    private QuotaLimitPrdDao quotaLimitPrdDao;
    @Autowired
    private SequenceService sequenceService;

    /**
     * 新增数据
     *
     * @param quotaLimitPrdVo
     * @return
     */
    @Override
    public int insert(QuotaLimitPrdVO quotaLimitPrdVo) {
        logger.debug("当前新增数据为:" + quotaLimitPrdVo.toString());
        int num = 0;
        try {
            quotaLimitPrdVo.setSerno(sequenceService.getSequence("LIMIT_NO", null, null));
            QuotaLimitPrd quotaLimitPrd = new QuotaLimitPrd();
            beanCopy(quotaLimitPrdVo, quotaLimitPrd);
            if (checkData(quotaLimitPrd)){
                return num = -2;
            }
            num = quotaLimitPrdDao.insert(quotaLimitPrd);
        } catch (Exception e) {
            logger.error("新增数据发生异常!", e);
            num = -1;
        }
        return num;
    }

    /**
     * 根据主键删除信息
     *
     * @param quotaLimitPrdVo
     * @return
     */
    @Override
    public int deleteByPk(QuotaLimitPrdVO quotaLimitPrdVo) {
        logger.debug("当前删除数据条件为:" + quotaLimitPrdVo);
        int num = 0;
        try {
            QuotaLimitPrd quotaLimitPrd = new QuotaLimitPrd();
            beanCopy(quotaLimitPrdVo, quotaLimitPrd);
            num = quotaLimitPrdDao.deleteByPk(quotaLimitPrd);
        } catch (Exception e) {
            logger.error("删除数据发生异常!", e);
            num = -1;
        }
        logger.debug("根据条件:" + quotaLimitPrdVo + "删除的数据条数为" + num);
        return num;
    }

    /**
     * 根据主键更新信息
     *
     * @param quotaLimitPrdVo
     * @return
     */
    @Override
    public int updateByPk(QuotaLimitPrdVO quotaLimitPrdVo) {
        logger.debug("当前修改数据为:" + quotaLimitPrdVo.toString());
        int num = 0;
        try {
            QuotaLimitPrd quotaLimitPrd = new QuotaLimitPrd();
            beanCopy(quotaLimitPrdVo, quotaLimitPrd);
            num = quotaLimitPrdDao.updateByPk(quotaLimitPrd);
        } catch (Exception e) {
            logger.error("修改数据发生异常!", e);
            num = -1;
        }
        logger.debug("根据条件:" + quotaLimitPrdVo + "修改的数据条数为" + num);
        return num;
    }

    /**
     * 根据主键查询详情
     *
     * @param inQuotaLimitPrdVo
     * @return
     */
    @Override
    public QuotaLimitPrdVO queryByPk(QuotaLimitPrdVO inQuotaLimitPrdVo) {
        logger.debug("当前查询参数信息为:" + inQuotaLimitPrdVo);
        try {
            QuotaLimitPrd queryQuotaLimitPrd = new QuotaLimitPrd();
            beanCopy(inQuotaLimitPrdVo, queryQuotaLimitPrd);
            QuotaLimitPrd queryRslQuotaLimitPrd = quotaLimitPrdDao.queryByPk(queryQuotaLimitPrd);
            if (Objects.nonNull(queryRslQuotaLimitPrd)) {
                return beanCopy(queryRslQuotaLimitPrd, new QuotaLimitPrdVO());
            } else {
                logger.debug("当前查询结果为空!");
            }
        } catch (Exception e) {
            logger.error("查询数据发生异常!", e);
        }
        return null;
    }


    @Override
    public int updateQuotaStsByPk(QuotaLimitPrdVO quotaLimitPrdVO) {
        logger.debug("当前修改数据为:" + quotaLimitPrdVO.toString());
        int num = 0;
        try {
            QuotaLimitPrd quotaLimitPrd = new QuotaLimitPrd();
            beanCopy(quotaLimitPrdVO, quotaLimitPrd);
            //检查是否已存在同客户类型，同限额类型的有效额度
            boolean isDup = checkData(quotaLimitPrd);
            if (isDup) return num = -2;
            num = quotaLimitPrdDao.updateQuotaStsByPk(quotaLimitPrd);
        } catch (Exception e) {
            logger.error("修改数据发生异常!", e);
            num = -1;
        }
        logger.debug("根据条件:"+ quotaLimitPrdVO +"修改的数据条数为"+ num);
        return num;
    }

    @Override
    public List<QuotaLimitPrdVO> queryAllByPage(QuotaLimitPrdVO quotaLimitPrdVO){
        logger.debug("当前查询参数信息为:");
        QuotaLimitPrd quotaLimitPrd = beanCopy(quotaLimitPrdVO, new QuotaLimitPrd());
        List<QuotaLimitPrd> quotaLimitPrds = quotaLimitPrdDao.queryAllByPage(quotaLimitPrd);
        logger.debug("当前查询结果集数量为:" + quotaLimitPrds.size());
        List<QuotaLimitPrdVO> list = null;
        try {
            pageSet(quotaLimitPrds, quotaLimitPrd);
            list = (List<QuotaLimitPrdVO>) beansCopy(quotaLimitPrds, QuotaLimitPrdVO.class);
        } catch (Exception e) {
            logger.error("数据转换出现异常!", e);
        }
        return list;
    }


    private boolean checkData(QuotaLimitPrd inQuotaLimitPrd) {
        if (LimitStatusEnum.QUOTA_LIMIT_STATUS_01.getKey().equals(inQuotaLimitPrd.getSts())) {
            List<QuotaLimitPrd> queryRs = quotaLimitPrdDao.queryByCondition(inQuotaLimitPrd);
            if (!Objects.isNull(queryRs) && !queryRs.isEmpty()){
                return true;
            }
        }
        return false;
    }

    @Override
    public List<QuotaLimitPrdVO> queryPrdActiveQuota(String prdId, String currentDate) throws Exception {
        return (List<QuotaLimitPrdVO>) beansCopy(quotaLimitPrdDao.queryPrdActiveQuota(prdId, currentDate), QuotaLimitPrdVO.class);
    }

    @Override
    public List<QuotaLimitPrdVO> queryAllCurrPrdByPage(QuotaLimitPrdVO quotaLimitPrdVO){
        logger.debug("当前查询参数信息为:");
        QuotaLimitPrd quotaLimitPrd = beanCopy(quotaLimitPrdVO, new QuotaLimitPrd());
        List<QuotaLimitPrd> quotaLimitPrds = quotaLimitPrdDao.queryAllCurrPrdByPage(quotaLimitPrd);
        logger.debug("当前查询结果集数量为:" + quotaLimitPrds.size());
        List<QuotaLimitPrdVO> list = null;
        try {
            pageSet(quotaLimitPrds, quotaLimitPrd);
            list = (List<QuotaLimitPrdVO>) beansCopy(quotaLimitPrds, QuotaLimitPrdVO.class);
        } catch (Exception e) {
            logger.error("数据转换出现异常!", e);
        }
        return list;
    }
}
